<?php

namespace app\api\model\hire;

use app\common\enum\order\OrderSourceEnum;
use app\common\exception\BaseException;
use app\common\model\hire\Hire as HireModel;
use app\common\library\helper;
use app\common\model\paper\RecordQuestion as RecordQuestionModel;

/**
 * 简历模型
 */
class Hire extends HireModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [];

    //获取列表
    public function getList($data)
    {
        $model = $this;
        if (isset($data['category_id']) && $data['category_id'] > 0) {
            $model = $model->where('category_id', '=', $data['category_id']);
        }
        if (isset($data['search']) && $data['search']) {
            $model = $model->where('title', 'like', '%' . $data['search'] . '%');
        }
        if (!empty($data['province_id'])) {
            $model = $model->where('province_id', '=',$data['province_id'] );
        }
        if (!empty($data['city_id'])) {
            $model = $model->where('city_id', '=',$data['city_id'] );
        }
        if (!empty($data['region_id'])) {
            $model = $model->where('region_id', '=',$data['region_id'] );
        }
        if (isset($data['type']) && $data['type'] > 0) {
            switch ($data['type']) {
                case 1:
                    $model = $model->where('sign_start_sime', '>', time());
                    break;
                case 2:
                    $model = $model->where('sign_start_sime', '<', time())->where('sign_end_sime', '>', time());
                    break;
                case 3:
                    $model = $model->where('sign_end_sime', '<', time());
                    break;
            }
        }
        $sort = ['sort' => 'asc', 'hire_id' => 'desc'];
        $list = $model->with(['category','image'])
            ->where('is_delete', '=', 0)
            ->where('is_show','=',0)
            ->order($sort)
            ->field("hire_id,title,sort,create_time,category_id,browse_count,sign_start_sime,sign_end_sime,image_id")
            ->paginate($data);
        foreach ($list as $k=>$v){
            if($v['sign_start_sime']>time()){
                $list[$k]['status_text'] = "即将报名";
            }
            if($v['sign_start_sime']<time() && $v['sign_end_sime']>time()){
                $list[$k]['status_text'] = "正在报名";
            }
            if($v['sign_end_sime']<time()){
                $list[$k]['status_text'] = "截至报名";
            }
        }
        return $list;
    }

    //简历详情
    public function getDetail($hire_id, $user = false)
    {
        $detail = self::detail($hire_id);
        if (!$detail) {
            throw new BaseException(['msg' => "招聘新信息不存在"]);
        }
        $this->addBrowse($hire_id);
        return $detail;
    }

    //添加浏览数
    public function addBrowse($hire_id)
    {
        $this->where('hire_id', '=', $hire_id)->inc('browse_count', 1)->update();
    }




    /**
     * 立即购买：获取订单简历列表
     */
    public static function getOrderCourseListByNow($params)
    {
        // 商品详情
        $course = self::detail($params['hire_id']);
        // 商品列表
        $courseList = [$course];
        foreach ($courseList as &$item) {
            // 商品购买数量
            $item['total_num'] = 1;
            // 商品购买数量
            $item['product_price'] = $item['course_price'];
            // 商品购买总金额
            $item['total_price'] = helper::bcmul($item['course_price'], $item['total_num']);
            //商品id
            $item['product_id'] = $item['hire_id'];
        }
        return $courseList;
    }

    //获取列表
    public function getListByIdsFromApi($id)
    {
        $list = $this->with(['image'])

            ->where('is_show', '=', 0)
            ->where('is_delete', '=', 0)
            ->where('hire_id', 'in', $id)
            ->order(['sort' => 'asc', 'hire_id' => 'desc'])
            ->select();
        return $list;
    }
}